<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <div id="app">
    <father></father>
  </div>
  <script src="./vue.js"></script>
  <script>
    // 定义一个 event bus 事件中心总线
    const bus = new Vue();
    const Child = {
      template:`
        <div>
          <h2>子组件1</h2>
          <button @click="emit">点击发送给我的兄弟</button>
        </div>
      `,
      data(){
        return {
          msg: '子组件1的数据'
        }
      },
      methods: {
        emit(){
          bus.$emit('biubiu', this.msg)
        }
      }
    }
    const Child2 = {
      template:`
        <div>
          <h2>子组件2</h2>
          {{ msg }}
        </div>
      `,
      data(){
        return {
          msg: '子组件2的数据'
        }
      },
      mounted(){
        bus.$on('biubiu', (msg) =>{
          this.msg = msg;
        })
      /*   bus.$on('biubiu', function(msg){
          this.msg = msg;
        }.bind(this)) */
      }
    }
    const Father = {
      template: `
        <div>
          <h1>这是父组件</h1>   
          
          <child></child>
          <hr/>
          <child2></child2>
        </div>
      `,
      components: {
        Child,
        Child2
      }
    };
    Vue.component('Father', Father)
    const vm = new Vue({
      el: '#app'
    })
  </script>
</body>
</html>